package org.example.java.base;

/**
 * @author juntao zhang
 */
public class ByteTest {
  public static void main(String[] args) {
    System.out.println(toString("zhang".getBytes()));

    System.out.println(toString("张".getBytes()));
    System.out.println(Integer.toBinaryString('z'));
    System.out.println(Integer.toBinaryString(0xFF));
  }

  public static String toString(byte[] a) {
    if (a == null)
      return "null";
    int iMax = a.length - 1;
    if (iMax == -1)
      return "[]";

    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++) {
      b.append(Integer.toBinaryString(a[i] & 0xFF));
      if (i == iMax)
        return b.append(']').toString();
      b.append(", ");
    }
  }
}
